package org.zoomdev.zoom.dao.adapters.parameter;

import org.zoomdev.zoom.common.json.JSON;
import org.zoomdev.zoom.dao.adapters.ParameterAdapter;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JsonParameterAdapter<T> implements ParameterAdapter<T> {

    @Override
    public void setNonnullParameter(PreparedStatement ps, int index, T data) throws SQLException {
        try {
            String json = JSON.stringify(data);
            ps.setString(index, json);
        } catch (Exception e) {
            throw new SQLException("Failed to convert object to JSON", e);
        }
    }
}
